package com.thp.erp.action;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.thp.erp.biz.IBaseBiz;

public class BaseAction<T> {
	
	private IBaseBiz<T> baseBiz;

	public void setBaseBiz(IBaseBiz<T> baseBiz) {
		this.baseBiz = baseBiz;
	}
	
	
	
	// 采用属性驱动的方式来获取查询条件  类型为实体类
	private T t1;
	public T getT1() {
		return t1;
	}
	public void setT1(T t1) {
		this.t1 = t1;
	}
	private T t2;
	public T getT2() {
		return t2;
	}
	public void setT2(T t2) {
		this.t2 = t2;
	}

	private Object param; // 扩展参数
	public Object getParam() {
		return param;
	}
	public void setParam(Object param) {
		this.param = param;
	}
	
	// 条件查询所有
	public void list() {
		List<T> list = baseBiz.getList(t1, t2, param);
		String jsonString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
		write(jsonString);
	}

	
	private int page;  // 页码
	private int rows;  // 每页记录数
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	

	/**
	 * 按照条件查询列表
	 */
	public void listByPage() {
		// List<T> list = baseBiz.getList(T1,T2,param);
		int firstResult = (page - 1) * rows;
		List<T> list = baseBiz.getListByPage(t1, t2, param, firstResult, rows);
	
		long count = baseBiz.getCount(t1, t2, param);
		Map map = new HashMap();
		map.put("rows", list);
		map.put("total", count);
		
		String jsonString = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
		write(jsonString);
	}
	
	
	
	
	private T t;
	public T getT() {
		return t;
	}
	public void setT(T t) {
		this.t = t;
	}
	/**
	 * 新增部门
	 */
	public void add() {
		try {
			System.out.println("-------BaseAction------");
			System.out.println(t);
			baseBiz.add(t);
			write(ajaxReturn(true, "添加成功"));
		} catch (Exception e) {
			e.printStackTrace();
			write(ajaxReturn(false, "发生异常"));
		}
		
	}
	
	
	// 修改
	public void update() {
		try {
			baseBiz.update(t);
			write(ajaxReturn(true, "修改成功"));
		} catch (Exception e) {
			write(ajaxReturn(false, "修改失败"));
		}
	}
	
	
	private Long id;   // 部门的ID
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	// 删除部门
	public void delete() {
		try {
			baseBiz.delete(id);
			write(ajaxReturn(true, "删除成功"));
		} catch (Exception e) {
			e.printStackTrace();
			write(ajaxReturn(false, "删除失败"));
		}
	}
	
	
	
	
	// 根据部门ID查询部门实体进行信息的回显
	public void get() {
		write(mapJson(JSON.toJSONString(baseBiz.get(id)),"t"));
	}
	
	
	
	
	private long[] ids;
	public long[] getIds() {
		return ids;
	}
	public void setIds(long[] ids) {
		this.ids = ids;
	}
	// 根据id删除
	public void deleByIds() {
		System.out.println("根据id批量删除");
		System.out.println(Arrays.toString(ids));
		
		try {
			baseBiz.deleteByIds(ids);
			write(ajaxReturn(true, "批量删除成功"));
		} catch (Exception e) {
			write(ajaxReturn(false, "批量删除失败"));
		}
		
	}
	
	
	
	/**************************工具方法******************************/
	/**
	 * 输出信息
	 */
	private void write(String jsonString) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		try {
			response.getWriter().print(jsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 构建ajax结构返回体
	 */
	private String ajaxReturn(boolean success, String message) {
		Map map = new HashMap<>();
		map.put("success", success);
		map.put("message", message);
		String jsonString = JSON.toJSONString(map);
		return jsonString;
	}
	
	/**
	 * 自定义添加前缀,页面信息的回显需要使用
	 */
	private String mapJson(String jsonString, String prefix) {
		Map<String, Object> map = JSON.parseObject(jsonString, Map.class);
		Map<String, Object> newMap = new HashMap();
		for(Entry<String, Object> entry : map.entrySet()) {
			newMap.put(prefix + "." + entry.getKey(), entry.getValue());
		}
		return JSON.toJSONString(newMap);
	}
	
	
	
	
}
